<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="所属字典" prop="parentId">
        <!--        <el-input readonl placeholder="所属字典"></el-input> -->
        <el-input @focus="handleParentDictTree()" placeholder="选择字典" readonly v-model="dataForm.parentName">
        </el-input>
        <input type="hidden" v-model="dataForm.parentId" />
      </el-form-item>
      <!--  <el-form-item label="编码" prop="type">
      <el-input v-model="dataForm.type" placeholder="编码"></el-input>
    </el-form-item> -->
      <el-form-item label="显示" prop="label">
        <el-input v-model="dataForm.label" placeholder="显示"></el-input>
      </el-form-item>
      <!--    <el-form-item label="val" prop="val">
      <el-input v-model="dataForm.val" placeholder="val"></el-input>
    </el-form-item> -->

      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :step="1" step-strictly></el-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="remake">
        <el-input v-model="dataForm.remake" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>


    <el-dialog :visible.sync="dialogDictVisible" title="选择字典" append-to-body>
      <el-input placeholder="输入关键字进行过滤" v-model="filterParentTreeDictText" @change="handleParentDictTree">
      </el-input>
      <el-tree :data="listTree" :default-checked-keys="checkedKeys" :filter-node-method="filterNode" @node-click="clickNodeSelectData"
        check-strictly class="filter-tree" default-expand-all highlight-current node-key="id" ref="selectParentDictTree">
      </el-tree>
    </el-dialog>
  </el-dialog>




</template>

<script>
  import util from "@/util/util";
  import validate from "@/util/validate";
  export default {
    props: {
      cancelReturn: {
        type: Function
      }
    },
    data() {
      return {
        pid:'',
        pname:'',
        checkedKeys: [],
        visible: false,
        dialogDictVisible: false,
        dataForm: {
          type: '',
          label: '',
          sellerId: '',
          val: '',
          parentId: '',
          sort: 0,
          remake: '',
          parentName: ''
        },
        filterParentTreeDictText: '',
        listTree: [],
        dataRule: {
          // type: [
          //   { required: true, message: '编码不能为空', trigger: 'blur' }
          // ],
          label: [{
            required: true,
            message: '显示不能为空',
            trigger: 'blur'
          }],

          // val: [
          //   { required: true, message: '数值不能为空', trigger: 'blur' }
          // ],
          parentId: [{
            required: true,
            message: '请选择父级节点',
            trigger: 'change'
          }]
        }
      }
    },
    created() {

    },
    methods: {
      clickNodeSelectData(data) {
        this.dataForm.parentId = data.id;
        this.dataForm.parentName = data.label;
        this.dialogDictVisible = false;
      },
      handleParentDictTree() {
        this.$http({
          url: this.$http.adornUrl(`/hotel/thotelagreementtype/listTrees`),
          method: 'get',
          params: this.$http.adornParams({
            name: this.filterParentTreeDictText,
          })

        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.listTree = [];
            this.listTree = util.parseTreeData(data.page);
            this.dialogDictVisible = true;
          }
        })
        // dictService.fetchTree().then(response => {
        //   this.treeDictSelectData = util.parseTreeData(response.data);

        //   setTimeout(() => {
        //     this.$refs['selectParentDictTree'].setCurrentKey(this.form.parentId ? this.form.parentId : null);
        //   }, 100)
        // })
      },
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1
      },
      remakcc() {
        this.dataForm = {
          type: '',
          label: '',
          sellerId: '',
          val: '',
          parentId: '',
          sort: '',
          remake: '',
          parentName: ''
        }
      },
      init(id,pid,pname) {
        Object.assign(this.$data, this.$options.data())
        this.remakcc();
        this.pid = pid
        this.pname = pname
        this.dataForm.parentId = this.pid;
        this.dataForm.parentName = this.pname;
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelagreementtype/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.tHotelAgreementType.type
                this.dataForm.label = data.tHotelAgreementType.label
                this.dataForm.sellerId = data.tHotelAgreementType.sellerId
                this.dataForm.val = data.tHotelAgreementType.val
                this.dataForm.parentId = data.tHotelAgreementType.parentId
                this.dataForm.sort = data.tHotelAgreementType.sort
                this.dataForm.remake = data.tHotelAgreementType.remake
                this.dataForm.parentName = data.tHotelAgreementType.parentName
                this.handleParentTree();
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        if (this.dataForm.parentId == "-1") {
          this.$message.error("请选择根以外的节点")
          return false
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelagreementtype/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'label': this.dataForm.label,
                'val': this.dataForm.val,
                'parentId': this.dataForm.parentId,
                'sort': this.dataForm.sort,
                'remake': this.dataForm.remake
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                    this.cancelReturn(this.dataForm.parentId);
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
